package com.df.happylearn.messages;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.df.happylearn.R;
import com.df.happylearn.model.MessageInfo;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class MessageListAdapter extends RecyclerView.Adapter<MessageListAdapter.MyViewHolder> {

    private Context mContext;
    private List<MessageInfo> mMessgeList;
    private List<Integer> mrReadIds;
    SimpleDateFormat sdf;

    public MessageListAdapter(Context context, List<MessageInfo> list, List<Integer> readIds) {
        mContext = context;
        mMessgeList = list;
        mrReadIds = readIds;
        sdf = new SimpleDateFormat("MM月dd日");
    }

    public void setReadIds(List<Integer> readIds) {
        mrReadIds = readIds;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_message, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        MessageInfo bean = mMessgeList.get(position);

        if (mrReadIds.contains(bean.mId)) {
            holder.article_img.setSelected(true);
            holder.mContent.setTextColor(mContext.getResources().getColor(R.color.color_message_read));
            holder.mTime.setTextColor(mContext.getResources().getColor(R.color.color_message_read));
        } else {
            holder.article_img.setSelected(false);
            holder.mContent.setTextColor(mContext.getResources().getColor(R.color.title_color));
            holder.mTime.setTextColor(mContext.getResources().getColor(R.color.title_color));
        }
        holder.mContent.setText(bean.mContent);
        //时间
        Date date = new Date(Long.parseLong(bean.mCtime));
        holder.mTime.setText(sdf.format(date)); //时间

        // register click event
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(holder.itemView, position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mMessgeList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView article_img;
        TextView mContent;
        TextView mTime;

        public MyViewHolder(View view) {
            super(view);
            article_img = (ImageView) view.findViewById(R.id.message_img);
            mContent = (TextView) view.findViewById(R.id.content);
            mTime = (TextView) view.findViewById(R.id.message_time);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickLitener(OnItemClickListener l) {
        this.mOnItemClickListener = l;
    }
}
